Overview

Azure WebJobs provide an easy way to run scripts or programs as background processes in the context of an App Service web app, API app, or mobile app. You can upload and run an executable file such as as cmd, bat, exe (.NET), ps1, sh, php, py, js and jar. These programs run as WebJobs on a schedule (cron), continuously or on a trigger such as the arrival of a queue message.

It's possible to significantly enhance a Dynamics 365 CRM instance using Azure WebJobs. See page "101 Ways to Enhance Dynamics CRM with Azure" and search for "webjob" to learn about just a few of the ways that using WebJobs can improve business and data processing.

Prerequisites

  • Microsoft Azure subscription
  • Microsoft Visual Studio 2015 or newer
  • Microsoft Azure SDK

Resources / Links

Azure WebJobs documentation resources

WebJobs SDK

The WebJobs SDK has built-in features for working with Azure Storage and Service Bus, for scheduling tasks and handling errors, and for many other common scenarios.

The WebJobs SDK includes the following components:
  • NuGet packages. NuGet packages that you add to a Visual Studio Console Application project provide a framework that your code uses by decorating your methods with WebJobs SDK attributes.
  • Dashboard. Part of the WebJobs SDK is included in Azure App Service and provides rich monitoring and diagnostics for programs that use the NuGet packages. You don't have to write code to use these monitoring and diagnostics features.

https://github.com/Azure/azure-webjobs-sdk/wiki

https://docs.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk

Creating an Azure WebJob that Interacts with Dynamics 365/CRM

This section provides a quick tutorial for creating an Azure WebJob using Visual Studio, .NET, C# and the Dynamics 365/CRM SDK. It also covers WebJob deployment and setting it to run (trigger) on a schedule.

For a more complete example that also includes a website, see article "Create a .NET WebJob in Azure App Service".

Create the base WebJob project and solution
  1. Run Visual Studio 2015
  2. Select File > New > Project. Navigate to Templates\Cloud and select Azure WebJob.
  3. Provide a name for the WebJob project and the location and name for the VS solution. Consider including "WebJob" in the name of the project. Click OK.
  4. Build the solution to verify that the WebJob in its most basic form can be built successfully.
  5. Update NuGet packages. Open the NuGet manager window, go to Updates, click/check "Select all packages", unselect the package "Microsoft.IdentityModel.Clients.ActiveDirectory", then click the Update button. Click the "Accept" button to continue with the package updates. (See below for information about using version 2.28* of Microsoft.IdentityModel.Clients.ActiveDirectory.)
  6. To support the ability to run the WebJob on an internal schedule, add the NuGet package Microsoft.Azure.WebJobs.Extensions to the project.
  7. To enable publishing the WebJob from Visual Studio, add the NuGet package Microsoft.Web.WebJobs.Publish.
  8. Rebuild the solution again and verify that there are no errors.
  9. Now is a good time to check in this base application into your source control system.

About the base WebJob project
  • When you create an Azure WebJob project, Visual Studio automatically includes a NuGet package reference to the WebJobs SDK (Microsoft.Azure.WebJobs.dll).
  • There are several other NuGet package references added by default for a WebJob project.
  • If your WebJob will not be triggered from a message in a queue, you can remove the ProcessQueueMessage method in Functions.cs or delete Functions.cs if you don't want to keep a file by that name. The file is intended for use with the WebJobs SDK Extensions.

Add the Dynamics 365 CRM SDK via NuGet
In order to connect to Dynamics 365 CRM from a .NET application, Microsoft recommends the use of the Microsoft Dynamics CRM SDK. The SDK includes a set of .NET "tooling" assemblies that make connecting to CRM easy by using a connection string. The steps below describe the procedures for connecting to CRM using the tooling assemblies.
  1. In Visual Studio within the WebJob project, right-click "References" and select "Manage NuGet Packages..."
  2. From the Browse page in the NuGet manager, search for "Microsoft.CrmSdk.XrmTooling.CoreAssembly". Select this NuGet package and click Install.
  3. In the NuGet manager, you might see that there are updates available for the .NET assemblies that were included in CRM SDK. It is generally a good idea to update the CRM SDK assemblies to the latest version, but if your WebJob will run on an older version of Dynamics CRM then be sure to avoid SDK functionality that is not compatible with the older version. See the SDK release history for details.

Sample CRM SDK Code for a Scheduled WebJob

See article "Use connection strings in XRM tooling to connect to Dynamics 365"

With Microsoft Dynamics 365 (online & on-premises), XRM tooling enables you to connect to your Dynamics 365 instance by using connection strings. This is similar to the concept of connection strings used with Microsoft SQL Server. Connection strings have native support in configuration files, including the ability to encrypt the configuration sections for maximum security. This enables you to configure Dynamics 365 connections at deployment time, and not hard code in your application to connect to your Dynamics 365 instance.

Example: Scheduling the WebJob to Run the CRM Operations Every 5 Minutes
In your Main method (the application entry point), include the following code line to enable the use of timers in your WebJobs application:

config.UseTimers();

In Functions.cs (or any class file), create a method that initializes a timer and calls the method when the timer expires (is triggered):

public static void RunMainProcess([TimerTrigger("0 */5 * * * *")] TimerInfo timerInfo)

When the WebJob runs, it will call RunMainProcess every five minutes.

Other Project Settings and Tasks before Deploying to Azure
  • From the project settings page, set the assembly name (e.g., "MyCorp.MyProject.CrmWebJob") and default namespace (e.g., "MyCorp.MyProject").
  • Consider renaming file Program.cs to a file name that includes "WebJob". This will help distinguish the file's purpose (the entry point to the WebJob) from other class file. If you rename the file, also change the class to something other than "Program" as well.
  • Consider creating a text file named "readme.txt" for your WebJobs project and storing project details such as the application overview, notes, links, deployment instructions, etc. You can also move the comments that Microsoft included in the base WebJob source code (in Program.cs) to this readme file and put them under a "TODO" section.
  • Turn on Application Logging (Blob) for the Web App that's hosting the WebJob. This will allow you to view logging/tracing details.
  • It's necessary to reference a 2.28* version of Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package, not 3.*. Otherwise, the application throws this exception when running in Azure: "System.TypeLoadException: Could not load type 'Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior' from assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory"


Deploy the WebJob to Azure
Right-click the project and select "Publish as Azure WebJob".

See also: "Deploy WebJobs using Visual Studio"

WebJobs Dashboard

The WebJobs SDK creates a dashboard website in Azure where it provides logging details. The URL for the dashboard is in this format: https://mywebapp.scm.azurewebsites.net/azurejobs/#/jobs

NuGet Packages added to a base WebJobs Project

The following are the NuGet packages that the Visual Studio 2015 WebJob template adds to a new WebJob project:
  • Microsoft.Azure.KeyValue.Core
  • Microsoft.Azure.WebJobs
  • Microsoft.Azure.WebJobs.Core
  • Microsoft.Data.Edm
  • Microsoft.Data.OData
  • Microsoft.Data.Services.Client
  • Microsoft.WindowsAzure.ConfigurationManager
  • Newtonsoft.Json
  • System.Spatial
  • WindowsAzure.Storage

Misc WebJob Info & Tips

  • The hosting web app for a WebJob has several environment variables and other settings that can be useful. For example, when logging an error from a WebJob, you can retrieve the WEBSITE_SITE_NAME value to include the website name with the error details. Here's a page that lists the environment variables and sample values: http://whatazurewebsiteenvironmentvariablesareavailable.azurewebsites.net/

.